package com.eke.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import com.jason.dbhelper2.jdbc.BaseJdbcTemplate;
import com.eke.bean.ArticleType;
import com.eke.bean.Comment;

@Repository("commentDao")
public class CommentDao extends BaseJdbcTemplate<Comment> {
	/**
	 * 点赞
	 * 
	 * @param commentId
	 * @return int
	 */
	public int goodNumber(int commentId) {
		String sql = "update comment set good_number = good_number + 1 where comment_id = ?";
		return this.update(sql, commentId);
	}

	/**
	 * 点踩
	 * 
	 * @param commentId
	 * @return int
	 */
	public int badNumber(int commentId) {
		String sql = "update comment set bad_number = bad_number + 1 where comment_id = ?";
		return this.update(sql, commentId);
	}

	/**
	 * 查询所有文章评论
	 * 
	 * @return {@link List}<{@link Comment}> 返回评论集合
	 */
	public List<Comment> qurryArticleComment(int articleId, int startRow, int pageSize) {
		String sql = "select * from comment  WHERE article_id = ? ORDER BY comment_id DESC limit ?,? ";
		return this.queryForList(sql, articleId, startRow, pageSize);
	}

	/**
	 * 查询评论的回复
	 * 
	 * @return {@link List}<{@link Comment}>
	 */
	public List<Comment> qurryCommentRq(int commentId, int startRow, int pageSize) {
		String sql = "select * from comment  WHERE reply_id = ? limit ?,? ";
		return this.queryForList(sql, commentId, startRow, pageSize);
	}

	/**
	 * 添加评论的回复
	 * 
	 * @return int
	 */
	public int addCommentRq(Comment comment) {
		return this.insert(comment);
	}

	/**
	 * 查询文章评论总数
	 * 
	 * @return int
	 */
	public int queryCommentRqTotal(int commentId) {
		String sql = "select count(1) total from comment WHERE reply_id=?";
		Map<String, Object> map = this.getJdbcTemplate().queryForMap(sql, commentId);
		return ((Long) map.get("total")).intValue();
	}

	/**
	 * 查询文章评论总数
	 * 
	 * @return int
	 */
	public int queryArticleCommentTotal(int articleId) {
		String sql = "select count(1) total from comment WHERE article_id=?";
		Map<String, Object> map = this.getJdbcTemplate().queryForMap(sql, articleId);
		return ((Long) map.get("total")).intValue();
	}

	/**
	 * 采纳评论
	 * 
	 * @param commentId
	 * @return {@link Boolean}
	 */
	public boolean updateCommentState(int commentId) {
		return this.update("UPDATE comment SET accept_state = 0 WHERE comment_id = ?", commentId) > 0;
	}

	/**
	 * 评论被采纳后，增加积分
	 * 
	 * @param interal
	 *            积分数量
	 * @param commentUserId
	 *            评论用户id
	 * @return {@link Boolean}
	 */
	public boolean addInteral(int interal, int commentUserId) {
		return this.update("UPDATE users set user_integral = user_integral + ? WHERE user_id = ?", interal,
				commentUserId) > 0;
	}

}
